﻿using FarmSmartSleep.Common.Domain.PM.select;
using FarmSmartSleep.Common.Dtos;
using FarmSmartSleep.Common.Infrastructure;
using MediatR;
using Microsoft.EntityFrameworkCore;

namespace FarmSmartSleep.Organ.API.Application.ProductionManagement.SelectFile
{
    /// <summary>
    /// 肥羊批次
    /// </summary>
    public class FatSheepBatchListHandler:IRequestHandler<FatSheepBatchListCommon,Result>
    {
        private readonly IBaseRepository<FatSheepBatch> fatSheepBatchRep;

        public FatSheepBatchListHandler(IBaseRepository<FatSheepBatch> fatSheepBatchRep)
        {
            this.fatSheepBatchRep = fatSheepBatchRep;
        }

        public async Task<Result> Handle(FatSheepBatchListCommon request, CancellationToken cancellationToken)
        {
            var result = new Result() { Code = (int)CodeEnum.search };
            try
            {
                var list = await fatSheepBatchRep.GetAll().ToListAsync();
                if (list.Count != 0)
                {
                    result.Data = list;
                    result.Code = (int)CodeEnum.success;
                }
                else
                {
                    result.Code = (int)CodeEnum.error;
                    result.Messege = "查询异常";
                }
            }
            catch (Exception)
            {
                result.Messege = "获取数据失败";
                throw;
            }
            return result;
        }
    }
}
